package com.netkit;

import com.netkit.message.Message;

public class MessageTask implements Runnable {
	private MessageDispatcher dispatcher;
	private Session session;
	private Message message;

	private MessageTask(MessageDispatcher dispatcher, Session session, Message msg) {
		this.dispatcher = dispatcher;
		this.session = session;
		this.message = msg;
	}

	public void run() {
		execute();
	}

	public void execute() {
		synchronized (dispatcher) {
			dispatcher.dispatch(session.getContext(), session, message);
		}
	}

	public static MessageTask newInstance(MessageDispatcher dispatcher, Session session, Message msg) {
		return new MessageTask(dispatcher, session,msg);
	}
}
